#!/bin/sh
# installation / upgrade script smartcard 2.0
# change 32/64 bits distributions below and maybe where to install into

appsrvuser=jboss
appsrvgrp=jboss

DISTFILES_INSTALL=/usr/local/install

INSTALL_INTO=/usr/local/certificate-services
export DISTFILES_INSTALL INSTALL_INTO

EJBCA_DISTNAME=ejbca_3_9_7.zip
EJBCA_DIRNAME=ejbca_3_9_7
EJBCA_SOFTLINK=ejbca
EJBCA_UNPACKCMD=unzip

EJBCAEXTRA_DISTNAME=extra_3_9_1.zip
EJBCAEXTRA_DIRNAME=extra_3_9_1
EJBCAEXTRA_SOFTLINK=extra
EJBCAEXTRA_UNPACKCMD=unzip

SMARTCARD20_DISTNAME=smartcard20-1.4.0.zip
SMARTCARD20_DIRNAME=smartcard20-1.4.0
SMARTCARD20_SOFTLINK=smartcard20
SMARTCARD20_UNPACKCMD=unzip

HTMF_DISTNAME=hardtokenmgmt_1.4.0.zip
HTMF_DIRNAME=hardtokenmgmt_1.4.0
HTMF_SOFTLINK=htmf
HTMF_UNPACKCMD=unzip
JARSIGNER=jarsigner.jks

HTMF_CUSTOM_DISTNAME=htmf_custom_project_1.4.0.zip
HTMF_CUSTOM_DIRNAME=htmf_custom_project_1.4.0
HTMF_CUSTOM_SOFTLINK=htmf_custom_project
HTMF_CUSTOM_UNPACKCMD=unzip

SYSCHECK_DISTNAME=syscheck-1.5.12.zip
SYSCHECK_DIRNAME=syscheck-1.5.12
SYSCHECK_SOFTLINK=syscheck
SYSCHECK_UNPACKCMD=unzip

JBOSS_DISTNAME=jboss-4.2.3.GA-jdk6.zip
JBOSS_DIRNAME=jboss-4.2.3.GA
JBOSS_SOFTLINK=jboss
JBOSS_UNPACKCMD=unzip
JBOSS_INITD=jboss-init.d

ANT_DISTNAME=apache-ant-1.7.1-bin.zip
ANT_DIRNAME=apache-ant-1.7.1
ANT_SOFTLINK=ant
ANT_UNPACKCMD=unzip

MYSQL_DISTNAME=mysql-connector-java-5.1.7.tar.gz
MYSQL_SOFTLINK=mysql-connector-java
MYSQL_DIRNAME=mysql-connector-java-5.1.7
MYSQL_JARNAME=mysql-connector-java-5.1.7-bin.jar
MYSQL_UNPACKCMD="tar zxvf"

# 32 bits
PCSCD_DISTNAME=pcsc-1.4.102-binary-x86-debian5.zip
# 64 bits
#PCSCD_DISTNAME=pcsc-1.4.102-binary-x86_64-sles10sp2.zip

PCSCD_DIRNAME=pcsc-1.4.102
PCSCD_SOFTLINK=pcsc
PCSCD_INITD=pcscd-usr-local-init.d
PCSCD_UNPACKCMD=unzip


# 32 bits
JAVA_DISTNAME=jdk-6u20-linux-i586.bin
# 64 bits
#JAVA_DISTNAME=jdk-6u20-linux-x64.bin
JAVA_DIRNAME=jdk1.6.0_20
JAVA_SOFTLINK=java
#JAVA_UNPACKCMD=sh
JAVA_UNPACKCMD=jdk-autoinst


JCE_DISTNAME=jce_policy-6.zip
JCE_DIRNAME=jce_policy-6
JCE_SOFTLINK=jce
JCE_UNPACKCMD=unzip

# 32 bits
OMNIKEYDRIVER_DISTNAME=ifdokccid_lnx-3.4.0.tar.gz
OMNIKEYDRIVER_DIRNAME=ifdokccid_lnx-3.4.0
# 64 bits
#OMNIKEYDRIVER_DISTNAME=ifdokccid_lnx_x64-3.4.0.tar.gz
#OMNIKEYDRIVER_DIRNAME=ifdokccid_lnx_x64-3.4.0

OMNIKEYDRIVER_SOFTLINK=ifdokccid
OMNIKEYDRIVER_UNPACKCMD="tar zxvf"

PRIMECARD_DISTNAME=PrimeCard-1.9.1-b3-java1.6.zip
PRIMECARD_DIRNAME=PrimeCard-trunk-java1.6
PRIMECARD_JARDIST=dist-1.6
PRIMECARD_SOFTLINK=primecard
PRIMECARD_UNPACKCMD=unzip

EXTRA_APPSRV_JARS="extra-appsrv-jars"
CUSTOM_UNPACK="custom-unpack-files.sh"


platform=`uname -p` 
if [ "x${platform}" != "xx86_64" ] ; then
    echo "you're running NOT on a 64 bit platform, aborting "
fi


runningUserId=`id --user`
if [ ${runningUserId} -ne 0 ] ; then
    echo "$0 must be run as root"
    exit
fi

if [ ! -d ${DISTFILES_INSTALL} ] ; then
    mkdir -p ${DISTFILES_INSTALL}
fi

if [ ! -d ${INSTALL_INTO} ] ; then
    mkdir -p ${INSTALL_INTO}
fi

############## installing packages  #########################


### smartcard 2.0
if [ ! -f ${DISTFILES_INSTALL}/${SMARTCARD20_DISTNAME} ] ; then
    echo "copying ${SMARTCARD20_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${SMARTCARD20_DISTNAME}   ${DISTFILES_INSTALL}
fi


if [ -f "${DISTFILES_INSTALL}/${SMARTCARD20_DISTNAME}" ] ;then
    cd ${INSTALL_INTO}
    ${SMARTCARD20_UNPACKCMD} ${DISTFILES_INSTALL}/${SMARTCARD20_DISTNAME}
    cd -
fi

rm -f ${SMARTCARD20_SOFTLINK} 
ln -s ${SMARTCARD20_DIRNAME} ${INSTALL_INTO}/${SMARTCARD20_SOFTLINK}

for file in $(find ${INSTALL_INTO}/smartcard20/ -type f) ; do
	perl -pi -e "s#/usr/local/environment#${INSTALL_INTO}/environment#gio" $file      
done

### java / jce
if [ ! -f ${DISTFILES_INSTALL}/${JAVA_DISTNAME} ] ; then
    echo "copying ${JAVA_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${JAVA_DISTNAME}     ${DISTFILES_INSTALL}
fi

if [ ! -f ${DISTFILES_INSTALL}/${JCE_DISTNAME} ] ; then
    echo "copying ${JCE_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${JCE_DISTNAME}       ${DISTFILES_INSTALL}
fi


if [ -x ${JAVA_DIRNAME} ] ;then
    echo "${JAVA_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${INSTALL_INTO}/${SMARTCARD20_SOFTLINK}/${JAVA_UNPACKCMD} ${DISTFILES_INSTALL}/${JAVA_DISTNAME}
    ${JCE_UNPACKCMD} ${DISTFILES_INSTALL}/${JCE_DISTNAME}
    mv ${INSTALL_INTO}/jce/* ${JAVA_DIRNAME}/jre/lib/security/
    rm -rf ${INSTALL_INTO}/jce
    cd -
fi


rm -f ${JAVA_SOFTLINK}
ln -s ${JAVA_DIRNAME} ${INSTALL_INTO}/${JAVA_SOFTLINK}


### ejbca

if [ ! -f ${DISTFILES_INSTALL}/${EJBCA_DISTNAME} ] ; then 	
    echo "copying ${EJBCA_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${EJBCA_DISTNAME}     ${DISTFILES_INSTALL}
fi


if [ -x ${EJBCA_DIRNAME} ] ;then
    echo "${EJBCA_DIRNAME} exists, skipping"
else

    cd ${INSTALL_INTO}
    ${EJBCA_UNPACKCMD} ${DISTFILES_INSTALL}/${EJBCA_DISTNAME} 
    cd -
fi


rm -f ${EJBCA_SOFTLINK}
ln -s ${EJBCA_DIRNAME} ${INSTALL_INTO}/${EJBCA_SOFTLINK}

### ejbca ext RA

if [ ! -f ${DISTFILES_INSTALL}/${EJBCAEXTRA_DISTNAME} ] ; then  
    echo "copying ${EJBCAEXTRA_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${EJBCAEXTRA_DISTNAME}     ${DISTFILES_INSTALL}
fi


if [ -x ${EJBCAEXTRA_DIRNAME} ] ;then
    echo "${EJBCAEXTRA_DIRNAME} exists, skipping"
else

    cd ${INSTALL_INTO}
    ${EJBCAEXTRA_UNPACKCMD} ${DISTFILES_INSTALL}/${EJBCAEXTRA_DISTNAME} 
    cd -
fi


rm -f ${EJBCAEXTRA_SOFTLINK}
ln -s ${EJBCAEXTRA_DIRNAME} ${INSTALL_INTO}/${EJBCAEXTRA_SOFTLINK}




### htmf

if [ ! -f ${DISTFILES_INSTALL}/${HTMF_DISTNAME} ] ; then
    echo "copying ${HTMF_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${HTMF_DISTNAME}      ${DISTFILES_INSTALL}
fi


if [ -x ${HTMF_DIRNAME} ] ;then
    echo "${HTMF_DIRNAME} exists, skipping"
else
   
    cd ${INSTALL_INTO}
    ${HTMF_UNPACKCMD} ${DISTFILES_INSTALL}/${HTMF_DISTNAME}
    cd -
fi


rm -f ${HTMF_SOFTLINK}
ln -s ${HTMF_DIRNAME} ${INSTALL_INTO}/${HTMF_SOFTLINK}

### jboss

if [ ! -f ${DISTFILES_INSTALL}/${JBOSS_DISTNAME} ] ; then
    echo "copying ${JBOSS_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${JBOSS_DISTNAME}     ${DISTFILES_INSTALL}
fi


if [ -x ${JBOSS_DIRNAME} ] ;then
    echo "${JBOSS_DIRNAME} exists, skipping"
else
    
cd ${INSTALL_INTO}
    ${JBOSS_UNPACKCMD} ${DISTFILES_INSTALL}/${JBOSS_DISTNAME}
cd -
fi

rm -rf ${JBOSS_SOFTLINK}
ln -s ${JBOSS_DIRNAME} ${INSTALL_INTO}/${JBOSS_SOFTLINK}

### ant 

if [ ! -f ${DISTFILES_INSTALL}/${ANT_DISTNAME} ] ; then
    echo "copying ${ANT_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${ANT_DISTNAME}   ${DISTFILES_INSTALL}
fi

if [ -x ${ANT_DIRNAME} ] ;then
    echo "${ANT_DIRNAME} exists, skipping"
else

cd ${INSTALL_INTO}
    ${ANT_UNPACKCMD} ${DISTFILES_INSTALL}/${ANT_DISTNAME}
cd -
fi

rm -f ${ANT_SOFTLINK}
ln -s ${ANT_DIRNAME} ${INSTALL_INTO}/${ANT_SOFTLINK}


### pcscd
if [ ! -f ${DISTFILES_INSTALL}/${PCSCD_DISTNAME} ] ; then
    echo "copying ${PCSCD_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${PCSCD_DISTNAME} ${DISTFILES_INSTALL}
fi


if [ -x ${PCSCD_DIRNAME} ] ;then
    echo "${PCSCD_DIRNAME} exists, skipping"
else

cd ${INSTALL_INTO}
    ${PCSCD_UNPACKCMD} ${DISTFILES_INSTALL}/${PCSCD_DISTNAME}
cd -
fi


rm -f ${INSTALL_INTO}/${PCSCD_SOFTLINK}
rm -f /usr/local/${PCSCD_SOFTLINK}
rm -f /usr/local/${PCSCD_SOFTLINK}
ln -s ${PCSCD_DIRNAME} ${INSTALL_INTO}/${PCSCD_SOFTLINK}
ln -s ${INSTALL_INTO}/${PCSCD_DIRNAME} /usr/local/${PCSCD_DIRNAME}
ln -s ${INSTALL_INTO}/${PCSCD_DIRNAME} /usr/local/${PCSCD_SOFTLINK}

### primecard

if [ ! -f ${DISTFILES_INSTALL}/${PRIMECARD_DISTNAME} ] ; then
    echo "copying ${PRIMECARD_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${PRIMECARD_DISTNAME} ${DISTFILES_INSTALL}
fi

if [ -x ${PRIMECARD_DIRNAME} ] ;then
    echo "${PRIMECARD_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${PRIMECARD_UNPACKCMD} ${DISTFILES_INSTALL}/${PRIMECARD_DISTNAME}
    cd -
fi

rm -f ${PRIMECARD_SOFTLINK}
ln -s ${PRIMECARD_DIRNAME} ${INSTALL_INTO}/${PRIMECARD_SOFTLINK}



### mysql java driver
if [ ! -f ${DISTFILES_INSTALL}/${MYSQL_DISTNAME} ] ; then
    echo "copying ${MYSQL_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${MYSQL_DISTNAME}   ${DISTFILES_INSTALL}
fi


if [ -x ${MYSQL_DIRNAME} ] ;then
    echo "${MYSQL_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${MYSQL_UNPACKCMD} ${DISTFILES_INSTALL}/${MYSQL_DISTNAME}
    cd -
fi


### extra  appsrv jars
if [ ! -d ${DISTFILES_INSTALL}/${EXTRA_APPSRV_JARS} ] ; then
    echo "copying ${EXTRA_APPSRV_JARS} to ${DISTFILES_INSTALL}"
    cp -r ${EXTRA_APPSRV_JARS}   ${DISTFILES_INSTALL}
fi


### htmf custom
if [ ! -f ${DISTFILES_INSTALL}/${HTMF_CUSTOM_DISTNAME} ] ; then
    echo "copying ${HTMF_CUSTOM_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${HTMF_CUSTOM_DISTNAME}   ${DISTFILES_INSTALL}
fi


if [ -x ${HTMF_CUSTOM_DIRNAME} ] ;then
    echo "${HTMF_CUSTOM_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${HTMF_CUSTOM_UNPACKCMD} ${DISTFILES_INSTALL}/${HTMF_CUSTOM_DISTNAME}
    cd -
fi

rm -f ${HTMF_CUSTOM_SOFTLINK}
ln -s ${HTMF_CUSTOM_DIRNAME} ${INSTALL_INTO}/${HTMF_CUSTOM_SOFTLINK}


### syscheck
if [ ! -f ${DISTFILES_INSTALL}/${SYSCHECK_DISTNAME} ] ; then
    echo "copying ${SYSCHECK_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${SYSCHECK_DISTNAME}   ${DISTFILES_INSTALL}
fi


if [ -x ${SYSCHECK_DIRNAME} ] ;then
    echo "${SYSCHECK_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${SYSCHECK_UNPACKCMD} ${DISTFILES_INSTALL}/${SYSCHECK_DISTNAME}
    cd -
fi

rm -f ${SYSCHECK_SOFTLINK}
ln -s ${SYSCHECK_DIRNAME} ${INSTALL_INTO}/${SYSCHECK_SOFTLINK}

perl -pi -e "s#/etc/ejbca/environment#${INSTALL_INTO}/environment#gio" ${INSTALL_INTO}/${SYSCHECK_DIRNAME}/resources.sh





### jar signer
if [ ! -f ${DISTFILES_INSTALL}/${JARSIGNER} ] ; then
    echo "copying ${JARSIGNER} to ${DISTFILES_INSTALL}"
    cp ${JARSIGNER}   ${DISTFILES_INSTALL}
fi


if [ -d ${INSTALL_INTO}/${HTMF_DIRNAME}/ ] ;then
    cp ${DISTFILES_INSTALL}/${JARSIGNER} ${INSTALL_INTO}/${HTMF_DIRNAME}/${JARSIGNER}
else
    echo "${INSTALL_INTO}/${HTMF_DIRNAME}/ dont exist, this is BIG TROUBLE exiting"
    exit
fi

### omnikey driver
if [ ! -f ${DISTFILES_INSTALL}/${OMNIKEYDRIVER_DISTNAME} ] ; then
    echo "copying ${OMNIKEYDRIVER_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${OMNIKEYDRIVER_DISTNAME}       ${DISTFILES_INSTALL}
fi


if [ -x ${OMNIKEYDRIVER_DIRNAME} ] ;then
    echo "${OMNIKEYDRIVER_DIRNAME} exists, skipping"
else
   
    cd ${INSTALL_INTO}
    ${OMNIKEYDRIVER_UNPACKCMD} ${DISTFILES_INSTALL}/${OMNIKEYDRIVER_DISTNAME}
    cd -
fi

### jboss init.d
if [ ! -f ${DISTFILES_INSTALL}/${JBOSS_INITD} ] ; then
    echo "copying ${JBOSS_INITD} to ${DISTFILES_INSTALL}"
    cp ${JBOSS_INITD}   ${DISTFILES_INSTALL}
fi

### pcscd init.d
if [ ! -f ${DISTFILES_INSTALL}/${PCSCD_INITD} ] ; then
    echo "copying ${PCSCD_INITD} to ${DISTFILES_INSTALL}"
    cp ${PCSCD_INITD}   ${DISTFILES_INSTALL}
fi


########## add user #######################

grep -q  $appsrvuser /etc/passwd 
if [ $? -ne 0 ] ; then
	# create a user
    groupadd $appsrvgrp
    useradd -m -g $appsrvgrp $appsrvuser
fi


############ init.d files ######################

### pcscd init.d
cp ${DISTFILES_INSTALL}/${PCSCD_INITD} /etc/init.d/pcscd-usr-local
perl -pi -e "s#pcscd_BIN=/usr/local/pcsc-1.4.102/sbin/pcscd#pcscd_BIN=${INSTALL_INTO}/pcsc-1.4.102/sbin/pcscd#gio"  /etc/init.d/pcscd-usr-local
chmod 755 /etc/init.d/pcscd-usr-local
if [ -f /etc/init.d/pcscd ] ;then
    mv /etc/init.d/pcscd /etc/init.d/pcscd-system-disabled-by-sc20
    chmod 644 /etc/init.d/pcscd-system-disabled-by-sc20
fi

if [ -d /etc/init.d/rc3.d/ ] ;then 
    rm -f /etc/init.d/rc3.d/S91pcscd-usr-local /etc/init.d/rc0.d/K03pcscd-usr-local /etc/init.d/rc6.d/K03pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc3.d/S91pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc0.d/K03pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc6.d/K03pcscd-usr-local
elif [ -d /etc/rc3.d/ ] ; then
    rm -f /etc/rc3.d/S91pcscd-usr-local /etc/rc0.d/K02pcscd-usr-local /etc/rc6.d/K03pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/rc3.d/S91pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/rc0.d/K02pcscd-usr-local
    ln -s /etc/init.d/pcscd-usr-local /etc/rc6.d/K02pcscd-usr-local
else
    echo "cant find rc3.d and friends to make links to /etc/init.d/jboss no autostart/shutdown will be made"
fi

#### jboss init.d 
cp ${DISTFILES_INSTALL}/${JBOSS_INITD} /etc/init.d/jboss
perl -pi -e "s#/usr/local/jboss#${INSTALL_INTO}/jboss#gio"  /etc/init.d/jboss
perl -pi -e "s#/usr/local/java/bin#${INSTALL_INTO}/java/bin#gio"  /etc/init.d/jboss
chmod 755 /etc/init.d/jboss
if [ -d /etc/init.d/rc3.d/ ] ;then 
    rm -f /etc/init.d/rc3.d/S92jboss /etc/init.d/rc0.d/K02jboss /etc/init.d/rc6.d/K02jboss
    ln -s /etc/init.d/jboss /etc/init.d/rc3.d/S92jboss
    ln -s /etc/init.d/jboss /etc/init.d/rc0.d/K02jboss
    ln -s /etc/init.d/jboss /etc/init.d/rc6.d/K02jboss
elif [ -d /etc/rc3.d/ ] ; then
    rm -f /etc/rc3.d/S92jboss /etc/rc0.d/K02jboss /etc/rc6.d/K02jboss
    ln -s /etc/init.d/jboss /etc/rc3.d/S92jboss
    ln -s /etc/init.d/jboss /etc/rc0.d/K02jboss
    ln -s /etc/init.d/jboss /etc/rc6.d/K02jboss
    
else
    echo "cant find rc3.d and friends to make links to /etc/init.d/jboss no autostart/shutdown will be made"
fi


############# start environment file ###################


# add environment sourcing to jboss users shell
jbossenvhome=`grep "${INSTALL_INTO}/environment" /home/${appsrvuser}/.bashrc` || echo 0
if [ "x${jbossenvhome}" = "x" ] ; then
    echo "# htmf env" >> /home/${appsrvuser}/.bashrc
    echo "if [ -f ${INSTALL_INTO}/environment ]; then " >> /home/${appsrvuser}/.bashrc
    echo "    . ${INSTALL_INTO}/environment " >> /home/${appsrvuser}/.bashrc
    echo "fi" >> /home/${appsrvuser}/.bashrc
fi


datestr=$(date)
echo "# environment file created $datestr by $0" > ${INSTALL_INTO}/environment
echo "export JBOSSUS=${appsrvuser}" >> ${INSTALL_INTO}/environment
echo "export JBOSSGRP=${appsrvgrp}" >> ${INSTALL_INTO}/environment
echo "export ANT_HOME=${INSTALL_INTO}/ant" >> ${INSTALL_INTO}/environment
echo "export HTMF_HOME=${INSTALL_INTO}/htmf" >> ${INSTALL_INTO}/environment
echo "export EJBCA_HOME=${INSTALL_INTO}/ejbca" >> ${INSTALL_INTO}/environment
echo "export JAVA_HOME=${INSTALL_INTO}/java" >> ${INSTALL_INTO}/environment
echo "export JBOSS_HOME=${INSTALL_INTO}/jboss" >> ${INSTALL_INTO}/environment
echo "export MYSQLDRIVER=${INSTALL_INTO}/mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar" >> ${INSTALL_INTO}/environment
echo "export PRIMECARD_HOME=${INSTALL_INTO}/primecard" >> ${INSTALL_INTO}/environment
echo "export LD_LIBRARY_PATH=${INSTALL_INTO}/primecard/dist-1.6-3.5/:${INSTALL_INTO}${PCSCD_DIRNAME}/lib" >> ${INSTALL_INTO}/environment
echo "export SC20_DISTFILES=${DISTFILES_INSTALL}" >> ${INSTALL_INTO}/environment
echo "export DISTFILES_INSTALL=${DISTFILES_INSTALL}" >> ${INSTALL_INTO}/environment
echo "export SYSCHECK_HOME=${INSTALL_INTO}/syscheck" >> ${INSTALL_INTO}/environment
echo "export SMARTCARD20_HOME=${INSTALL_INTO}/smartcard20" >> ${INSTALL_INTO}/environment
echo "export ANT_OPTS='-Xms512m -Xmx512m'" >> ${INSTALL_INTO}/environment
echo "export JAVA_OPTS='-Xms512m -Xmx512m -XX:MaxPermSize=256m'" >> ${INSTALL_INTO}/environment
echo "export PATH=${INSTALL_INTO}/java/bin:${INSTALL_INTO}/ant/bin:${PATH}" >> ${INSTALL_INTO}/environment
echo "export EJBCA_DIRNAME=${EJBCA_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export HTMF_DIRNAME=${HTMF_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export JBOSS_DIRNAME=${JBOSS_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export JBOSS_DISTNAME=${JBOSS_DISTNAME}" >> ${INSTALL_INTO}/environment
echo "export ANT_DIRNAME=${ANT_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export HTMF_CUSTOM_DIRNAME=${HTMF_CUSTOM_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export PCSCD_DIRNAME=${PCSCD_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export JAVA_DIRNAME=${JAVA_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export PRIMECARD_DIRNAME=${PRIMECARD_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export INSTALL_INTO=${INSTALL_INTO}" >> ${INSTALL_INTO}/environment
echo "export EXTRA_APPSRV_JARS=${EXTRA_APPSRV_JARS}" >> ${INSTALL_INTO}/environment

source  ${INSTALL_INTO}/environment

ln -s ${INSTALL_INTO}/environment /usr/local/environment

#### end environment file ####


####### misc ##############
# change owner of the files
chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${PRIMECARD_DIRNAME}
chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${HTMF_DIRNAME}
chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${EJBCA_DIRNAME}
chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${JBOSS_DIRNAME}

# default syscheck install
${INSTALL_INTO}/${SYSCHECK_DIRNAME}/misc/syscheck-default-setup.sh

### primecard env settings
chmod 755 ${INSTALL_INTO}/${PRIMECARD_DIRNAME}/bin/*.sh
rm -rf ${INSTALL_INTO}/${PRIMECARD_DIRNAME}/bin/*.bat


if [ -f /usr/sbin/pcscd -o -f ${INSTALL_INTO}/pcsc/sbin/pcscd ] ; then 
    echo "pcscd is installed, will add support for primecardHSM (to store the CA:s private keys on a smartcard)"
    cd ${INSTALL_INTO}/${OMNIKEYDRIVER_DIRNAME}
    if [ -d ${INSTALL_INTO}/pcsc/pcsc/drivers ] ; then
	./install -d ${INSTALL_INTO}/pcsc/pcsc/drivers
    elif [ -d /usr/lib64/readers ] ; then
	./install -d /usr/lib64/readers
    elif [ -d /usr/lib/pcsc/drivers/ ] ; then
	./install -d /usr/lib/pcsc/drivers/
    fi
    /etc/init.d/pcscd-usr-local restart	 || echo "failed to restart pcscd"
    cd -
else
    echo "pcscd is NOT installed will not add suport for primecardHSM (to store the CA:s private keys on a smartcard)"
    echo "in debian: apt-get install pcscd"
    echo "in suse: add pcscd from custom build (we use 1.4.102)"
    echo "then rerun this script $0"
fi

############## custom-unpacking ##########################

./${CUSTOM_UNPACK}



############ end ##################################


echo "install of files done, to run a setup do:"
echo "cd ${INSTALL_INTO}/${SMARTCARD20_SOFTLINK}"
echo "to setup a new installation (no prior database): sudo ./smartcard2.0-setup"
echo "to upgrade from a previous version: sudo ./smartcard2.0-upgrade"


